import {
    ADD_CONTER, REDUCE_CONTER, GET_TO_DO_LIST,DEL_TO_DO_ITEM,
    MODIFY_TO_DO_PRICE,ADD_TO_DO_ITEM
} from './mutation-types';

export interface Modify {
    name?: string;
    id?: number;
    price: number | string;
}

const mutations = {

    [ADD_CONTER](state: StoreTestModel.Base): void {
        state.couter++
    },
    
    [REDUCE_CONTER](state: StoreTestModel.Base): void {
        state.couter--
    },
    // todolist
    [GET_TO_DO_LIST](state: StoreTestModel.Base,data: AjaxDataModel.DataKey[]): void {
        state.toDoList = data
    },
    [ADD_TO_DO_ITEM](state: StoreTestModel.Base, ite: Modify): void {
        let hasItem = state.toDoList.some((item) => {
            return item.name == ite.name
        })
        if (!hasItem) {
            state.toDoList.push(
                {
                    id: state.toDoList.length + 1,
                    ...ite
                }
            )
        } else {
            alert('已有该商品')
        }
    },

    [DEL_TO_DO_ITEM](state: StoreTestModel.Base,id: number): void {
        state.toDoList = state.toDoList.filter((item) => {
            return item.id != id
        } )
    },

    [MODIFY_TO_DO_PRICE](state: StoreTestModel.Base, ite: Modify): void {
        
        let index =  state.toDoList.findIndex((item) => {
            return item.id == ite.id
        })
        state.toDoList[index].price = ite.price
    },


}

export default mutations